package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Resource
    private UserInfoFeignClient userInfoFeignClient;
    @Resource
    private CategoryFeignClient categoryFeignClient;
    @Resource
    private RedissonClient redissonClient;
    /**
     * 获取专辑详情
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItem(Long albumId) {
        HashMap<String, Object> result = new HashMap<>();
        //查询布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)) {
            log.error("用户查询专辑不存在：{}", albumId);
            //查询数据不存在直接返回空对象
            return result;
        }
        //1、获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfoRes = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoRes, "远程调用获取专辑信息失败");
            AlbumInfo albumInfo = albumInfoRes.getData();
            Assert.notNull(albumInfo, "远程获取albumInfo失败");
            result.put("albumInfo", albumInfo);
            return albumInfo;
        }).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return null;
        });
        CompletableFuture<Integer> categoryCompletableFuture = albumInfoCompletableFuture.thenApplyAsync((albumInfo) -> {
            //2.获取分类信息
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            //判断
            Assert.notNull(categoryViewResult, "远程调用获取分类信息失败");
            BaseCategoryView categoryView = categoryViewResult.getData();
            //判断
            Assert.notNull(categoryView, "远程获取baseCategoryView失败");
            //整合
            result.put("baseCategoryView", categoryView);
            //  默认处理成功返回1；
            return 1;
        }).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        CompletableFuture<Integer> albumStatVoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //3.获取专辑统计信息
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStat(albumId);
            //判断
            Assert.notNull(albumStatVoResult, "远程调用获取专辑统计信息失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            //判断
            Assert.notNull(albumStatVo, "远程获取albumStatVo失败");
            //整合
            result.put("albumStatVo", albumStatVo);
            //  默认处理成功返回1；
            return 1;
        }).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        CompletableFuture<Integer> userInfoVoCompletableFuture = albumInfoCompletableFuture.thenApplyAsync((albumInfo) -> {
            //4.获取主播信息
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            //判断
            Assert.notNull(userInfoVoResult, "远程调用获取主播信息失败");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            //判断
            Assert.notNull(userInfoVo, "远程获取userInfoVo失败");
            //整合
            result.put("announcer", userInfoVo);
            //  默认处理成功返回1；
            return 1;
        }).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });

        //  获取每个线程执行的结果;
        try {
            if(null == albumInfoCompletableFuture.get() || 0==categoryCompletableFuture.get()
                    || 0== albumStatVoCompletableFuture.get() || 0==userInfoVoCompletableFuture.get()){
                return result;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        //多任务组合
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                albumStatVoCompletableFuture,
                userInfoVoCompletableFuture
        ).join();
        //需要远程调用
        return result;
    }
}
